// Problem 064: Odd period square roots

package main

import (
	"fmt"
	"math"
)

func p064() {
	ans := 4
	for a := 14; a <= 10000; a++ {
		b := int(math.Sqrt(float64(a)))
		if b*b == a {
			continue
		} else if n := handle064(a, b, 1, b, 0); n%2 == 1 {
			ans++
		}
	}
	fmt.Println("Problem 064:", ans)
}

// sqrt(a)-b/c, d equals b, as a flag to judge start, e to keep period length.
func handle064(a, b, c, d, e int) int {
	if (a-b*b)%c != 0 {
		panic("Error")
	}
	nextc, nextb := (a-b*b)/c, b+d
	nextb = d - nextb%nextc
	e++
	if nextb == d && nextc == 1 {
		return e
	} else {
		return handle064(a, nextb, nextc, d, e)
	}
}
